package com.wuseguang.springcloud.dao.jdbc;

import com.wuseguang.springcloud.JdbcTemplate.BaseTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.util.StringUtils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;


@Repository
public class AttendJdbc extends BaseTemplate {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    public List<Map<String, Object>> queryAttendListMap(String sql, Object... args) {
        logger.info("AttendJdbc==>queryAttendListMap,sql={}", sql);
        return dao.query(sql, args, rs -> {
            List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
            while (rs.next()) {
                Map<String, Object> map = new HashMap<String, Object>();
                int tid = rs.getInt("tid");
                map.put("tid", tid);
                map.put("attend_group_id", rs.getString("attend_group_id"));
                map.put("shift_id", rs.getString("shift_id"));
                map.put("attendtime", rs.getString("attendtime"));
                map.put("attendtype", rs.getString("attendtype"));
                list.add(map);
            }
            logger.info("AttendJdbc==>queryAttendListMap,maps={}", list.toString());
            return list;
        });
    }


    public List<String> queryAttendList(String sql, Object... args) {
        logger.info("AttendJdbc==>queryAttendListMap,sql={}", sql);
        return dao.query(sql, args, rs -> {
            List<String> list = new ArrayList<>();
            while (rs.next()) {
                if ("1".equals(rs.getString("type"))){
                    list.add(rs.getString("starttime_one"));
                    list.add(rs.getString("endtime_one"));
                    list.add(rs.getString("starttime_two"));
                    list.add(rs.getString("endtime_two"));
                }else {
                    list.add(rs.getString("starttime_one"));
                    list.add(rs.getString("endtime_two"));
                }
            }
            return list;
        });
    }


    public Map<String, String> queryAttendMap(String sql, Object... args) {
        logger.info("AttendJdbc==>queryAttendListMap,sql={}", sql);
        return dao.query(sql, args, rs -> {
            Map<String, String> map = new HashMap<String, String>();
            while (rs.next()) {
                String userid = rs.getString("userid");
                String attend = rs.getString("attend");
                map.put(userid, attend);
            }
            return map;
        });
    }

}
